#include "s3c2440.h"
#include "uart.h"
#include "rtc.h"

void RtcWrite(TIME_STRUCT Time)
{
    unsigned char TimeTmp = 0;
    bRTCCON |= RTCEN;

    TimeTmp = (Time.Second%10) & 0x0f;
    TimeTmp |=(Time.Second/10) << 4;
    bBCDSEC = (TimeTmp);

    TimeTmp = (Time.Minute % 10) & 0xf;
    TimeTmp |=(Time.Minute / 10) << 4;
    bBCDMIN = TimeTmp;

    TimeTmp = (Time.Hour % 10) & 0xf;
    TimeTmp |=(Time.Hour / 10) << 4;
    bBCDHOUR = TimeTmp;

    TimeTmp = Time.WeekDay & 0x7;
    //TimeTmp |=(minute / 10) << 4;
    bBCDDAY = TimeTmp;

    TimeTmp = (Time.Date % 10) & 0xf;
    TimeTmp |= (Time.Date / 10) << 4;
    bBCDDATE = TimeTmp;

    TimeTmp = (Time.Month % 10) & 0xf;
    TimeTmp |= (Time.Month / 10) << 4;
    bBCDMON = TimeTmp;

    bBCDYEAR = Time.Year;

    bRTCCON &= ~(RTCEN);
}
void RtcRead(TIME_STRUCT *Time)
{
    unsigned char TimeTmp;
    //TIME_STRUCT Time;
    //unsigned char Second,Minute,Hour,Date,WeekDay,Month,Year;

    bRTCCON |= RTCEN;

    TimeTmp = bBCDSEC;
    Time->Second = (TimeTmp >> 4) * 10 + (TimeTmp & 0xf);
    //printf("Second is %d\n", Second);

    TimeTmp = bBCDMIN;
    Time->Minute = (TimeTmp >> 4) * 10 + (TimeTmp & 0xf);
    //printf("Minute is %d\n", Minute);

    TimeTmp = bBCDHOUR;
    Time->Hour = (TimeTmp >> 4) * 10 + (TimeTmp & 0xf);
   // printf("Hour is %d\n", Hour);

    TimeTmp = bBCDDATE;
    Time->Date = (TimeTmp >> 4) * 10 + (TimeTmp & 0xf);
   // printf("Date is %d\n", Date);

    TimeTmp = bBCDDAY;
    Time->WeekDay = (TimeTmp & 0x7);
   // printf("WeekDay is %d\n", WeekDay);

    TimeTmp = bBCDMON;
    Time->Month = (TimeTmp >> 4) * 10 + (TimeTmp & 0xf);
    //printf("Month is %d\n", Month);

    //Year = bBCDYEAR;
    TimeTmp = bBCDYEAR;
    Time->Year = (TimeTmp >> 4) * 10 + (TimeTmp & 0xf);
   // printf("Year is %d\n", Year);
    bRTCCON &= ~(RTCEN);
    return;
}

void SetAlmTime(TIME_STRUCT AlmTime)
{
    unsigned char TimeTmp;

    bRTCALM = 0X7F;

    TimeTmp = (AlmTime.Second%10) & 0x0f;
    TimeTmp |=(AlmTime.Second/10) << 4;
    bALMSEC = (TimeTmp);

    TimeTmp = (AlmTime.Minute % 10) & 0xf;
    TimeTmp |=(AlmTime.Minute / 10) << 4;
    bALMMIN = TimeTmp;

    TimeTmp = (AlmTime.Hour % 10) & 0xf;
    TimeTmp |=(AlmTime.Hour / 10) << 4;
    bALMHOUR = TimeTmp;

    TimeTmp = (AlmTime.Date % 10) & 0xf;
    TimeTmp |= (AlmTime.Date / 10) << 4;
    bALMDATE = TimeTmp;

    TimeTmp = (AlmTime.Month % 10) & 0xf;
    TimeTmp |= (AlmTime.Month / 10) << 4;
    bALMMON = TimeTmp;

    bALMYEAR = AlmTime.Year;

}

